//-----------------------------------------------------------------------

// <copyright file="DialogFieldDefinition.cs" company="Jamie Knight">

// Copyright (c) Jamie Knight. All rights reserved.

// </copyright>

//-----------------------------------------------------------------------

using System;
using System.Windows.Controls;
using System.Windows.Data;

namespace DevKnightlie.WPF.AutoForm.FieldDefinitions
{
    public class DialogFieldDefinition: BaseFieldDefinition
    {

        #region Private Fields

        Func<object, object> _dialogAction;
        Binding _displayBinding = null;
        TextBox _textBox  =null;
        private bool _textBoxReadOnly = true;
        private string _buttonCaption = "...";
        private Button _chooseButton;

        #endregion


        #region Properties

        /// <summary>
        /// Sets or returns a delegate function that is called when the user clicks the action button
        /// </summary>
        public Func<object, object> DialogAction
        {
            get { return _dialogAction; }
            set { _dialogAction = value; }
        }


        /// <summary>
        /// Sets or returns a value indicating whether the text box is read-only
        /// </summary>
        public bool TextBoxReadOnly
        {
            get { return _textBoxReadOnly; }
            set { _textBoxReadOnly = value; }
        }


        /// <summary>
        /// Sets or returns the caption to apply to the action button
        /// </summary>
        public String ButtonCaption
        {
            get { return _buttonCaption; }
            set { _buttonCaption = value; }
        }


        /// <summary>
        /// Returns the action Button control
        /// </summary>
        public Button ActionButton
        {
            get { return _chooseButton; }
            private set { _chooseButton = value; }
        }


        #endregion

        #region Methods

        public override void BuildInputControl()
        {
            ContentControl result = new ContentControl()
            {
                Focusable = false
            };
            Grid layoutGrid = new Grid()
            {
                HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch
            };
            layoutGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star) });
            layoutGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new System.Windows.GridLength(1, System.Windows.GridUnitType.Auto) });
            layoutGrid.RowDefinitions.Add(new RowDefinition());
            _textBox = new TextBox()
            {
                IsReadOnly = TextBoxReadOnly,
                VerticalContentAlignment=System.Windows.VerticalAlignment.Center
            };
            _displayBinding = new Binding(PropertyName);
            BindingOperations.SetBinding(_textBox, TextBox.TextProperty, _displayBinding);
            _chooseButton = new Button()
            {
                Content = ButtonCaption,
                Margin = new System.Windows.Thickness(4, 0, 0, 0),
                Padding = new System.Windows.Thickness(8, 4, 8, 4),
                VerticalContentAlignment = System.Windows.VerticalAlignment.Center
            };
            _chooseButton.Click += chooseButton_Click;
            SetupEnabledBinding(_textBox);
            SetupEnabledBinding(_chooseButton);
            layoutGrid.Children.Add(_textBox);
            layoutGrid.Children.Add(_chooseButton);
            Grid.SetColumn(_textBox, 0);
            Grid.SetRow(_textBox, 0);
            Grid.SetColumn(_chooseButton, 1);
            Grid.SetRow(_chooseButton, 0);
            result.Content = layoutGrid;
            ContainerControl = result;
            InputControl = _textBox;
            ActionButton = _chooseButton;
        }

        void chooseButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (DialogAction == null)
            {
                return;
            }
            BindingExpression binding = _textBox.GetBindingExpression(TextBox.TextProperty);
            object boundObject = binding.ResolvedSource;

            object currentValue = boundObject.GetType().GetProperty(_displayBinding.Path.Path).GetValue(boundObject);
            object newValue = DialogAction(currentValue);
            if (newValue != null)
            {
                boundObject.GetType().GetProperty(_displayBinding.Path.Path).SetValue(boundObject, newValue);
            }
        }

        #endregion
    }
}
